package com.itheima.service.impl;

import com.itheima.dao.BlogMapper;
import com.itheima.dao.BlogSortMapper;
import com.itheima.dao.CommentMapper;
import com.itheima.dao.WebVisitMapper;
import com.itheima.service.IndexService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private WebVisitMapper webVisitMapper;

    @Autowired
    private BlogSortMapper blogSortMapper;

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 获取一年内每天的文章贡献度
     *
     * @return
     */
    @Override
    public Map getBlogContributeCount() {
        Date endTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        Date startTime = calendar.getTime();
        List<Map> resultList = blogMapper.getBlogContributeCount(startTime, endTime);
        List<List> blogContributeCount = new ArrayList<>();
        for (Map map : resultList) {
            List inList = new ArrayList();
            inList.add(map.get("date"));
            inList.add(map.get("count"));
            blogContributeCount.add(inList);
        }
        Map map = new HashMap();
        map.put("blogContributeCount", blogContributeCount);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List timeList = new ArrayList();
        timeList.add(simpleDateFormat.format(startTime));
        timeList.add(simpleDateFormat.format(endTime));
        map.put("contributeDate", timeList);
        return map;
    }

    /**
     * 获取每个分类下文章数目
     *
     * @return
     */
    @Override
    public List<Map> getBlogCountByBlogSort() {
        List<Map> list = blogSortMapper.getBlogCountByBlogSort();
        return list;
    }

    /**
     * 获取每个标签下文章数目
     *
     * @return
     */
    @Override
    public List<Map> getBlogCountByTag() {
        List<Map> list = blogMapper.getBlogCountByTag();
        return list;
    }

    /**
     * 获取最近一周用户独立IP数和访问量
     *
     * @return
     */
    @Override
    public Map<String, List> getVisitByWeek() {
        ArrayList<String> days = DateUtils.getDaysByN(7, "yyyy-MM-dd");
        //用户独立IP数集合
        List<Integer> uvList = new ArrayList<>();
        //访问量集合
        List<Integer> pvList = new ArrayList<>();
        for (String day : days) {
            String startTime = day + " 00:00:00";
            String endTime = day + " 23:59:59";
            Integer uv = webVisitMapper.getUv(startTime, endTime);
            uvList.add(uv);
            Integer pv = webVisitMapper.getPv(startTime, endTime);
            pvList.add(pv);
        }
        Map<String, List> map = new HashMap();
        map.put("date", DateUtils.getDaysByN(7, "MM-dd"));
        map.put("uv", uvList);
        map.put("pv", pvList);
        return map;
    }

    /**
     * 初始化数据,获取每天访问ip数,用户数,评论数,文章数
     *
     * @return
     */
    @Override
    public Map<String, Integer> init() {
        String startTime = DateUtils.getToDayStartTime();
        String endTime = DateUtils.getToDayEndTime();
        //当日访问的用户数
        Integer pv = webVisitMapper.getPv(startTime, endTime);
        //当日访问的ip
        Integer uv = webVisitMapper.getUv(startTime, endTime);
        //当日发布的博客数量
        Integer blogCount = blogMapper.getBlogCount4Day(startTime, endTime);
        //当日发布的评论数量
        Integer commentCount = commentMapper.getCommentCount4Day(startTime, endTime);
        //封装数据
        Map<String,Integer> resultMap = new HashMap<>();
        resultMap.put("blogCount",blogCount);
        resultMap.put("commentCount",commentCount);
        resultMap.put("visitCount",uv);
        resultMap.put("userCount",pv);
        return resultMap;
    }


}
